﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class cap_nhat_thong_tin_nguoi_dung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["TenDangNhap"] != null) // da dang nhap
            {
                string user = Session["TenDangNhap"].ToString();

                // truy xuat thong tin 
                using (babydealEntities context = new babydealEntities())
                {
                    //thanh_vien obj = new thanh_vien();
                    var kq = from s in context.thanh_vien
                             where s.ten_dang_nhap == user
                             select s;
                    if (kq.Count() != 0)
                    {
                        // String to DateTime
                        thanh_vien s = kq.First();
                        DateTime MyDateTime = DateTime.Parse(kq.First().ngay_sinh.ToString());
                        
                        tbxEmail.Text = s.email;
                        tbxMatKhau.Text = s.mat_khau;
                        tbxXacNhanMatKhau.Text = s.mat_khau;
                        tbxNgay.Text = MyDateTime.Day.ToString();
                        tbxThang.Text = MyDateTime.Month.ToString();
                        tbxNam.Text = MyDateTime.Year.ToString();
                        ddlGioiTinh.SelectedValue = s.gioi_tinh.ToString();
                        tbxHoTen.Text = s.ho_ten;
                        tbxDiaChi.Text = s.dia_chi;
                        tbxDienThoai.Text = s.so_dt;

                    }

                }


            }
        }
    }
    // kiem tra dang ky
    public Boolean kiemTraDangKy(string ten_dang_nhap)
    {
        using (babydealEntities context = new babydealEntities())
        {
            thanh_vien obj = new thanh_vien();
            var kq = from s in context.thanh_vien
                     where s.ten_dang_nhap == ten_dang_nhap
                     select s;
            if (kq.Count() != 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

    // nut dang ky
    protected void btnSubmit(object sender, EventArgs e)
    {

        
        string Email = tbxEmail.Text;

        string MatKhau = tbxMatKhau.Text;
        string XacNhanMatKhau = tbxXacNhanMatKhau.Text;

        string NgaySinh = tbxNgay.Text;
        string ThangSinh = tbxThang.Text;
        string NamSinh = tbxNam.Text;

        int GioiTinh = Convert.ToInt32(ddlGioiTinh.SelectedValue);

        string HoTen = tbxHoTen.Text;
        string DiaChi = tbxDiaChi.Text;
        string DienThoai = tbxDienThoai.Text;


        if (MatKhau == XacNhanMatKhau)
        {
            // dang ky
            using (babydealEntities context = new babydealEntities())
            {
                int id = Convert.ToInt32(Session["IDThanhVien"].ToString());
                var kq = from s in context.thanh_vien
                         where s.id == id
                         select s;
                thanh_vien tv = kq.First();
                
                tv.email = Email;
                tv.mat_khau = MatKhau;
                string myDate = NamSinh + "-" + ThangSinh + "-" + NgaySinh;
                DateTime myDateTime = DateTime.Parse(myDate);

                tv.ngay_sinh = myDateTime;
                tv.ho_ten = HoTen;
                tv.dia_chi = DiaChi;
                tv.so_dt = DienThoai;
                tv.gioi_tinh = GioiTinh;
                context.SaveChanges();
                //SendEmail.sendMailRegister(Email, TenDangNhap, TenDangNhap, MatKhau);
                if (tv.id != 0)
                {
                    Session["TenDangNhap"] = null;
                    Session["LanTruyCapCuoi"] = null;
                    Session["IDThanhVien"] = null;

                    
                    Response.Redirect("login.aspx");
                }

            }
        }



    }
}